package com.zsk.daily.accumulation.biz.manager;

import cn.hutool.core.collection.CollUtil;
import com.zsk.daily.accumulation.core.service.user.UserInfoService;
import com.zsk.daily.accumulation.dal.model.UserInfoDO;
import com.zsk.daily.accumulation.web.model.request.user.GetUserInfoRequest;
import com.zsk.daily.accumulation.web.model.result.user.GetUserInfoResult;
import com.zsk.daily.accumulation.web.model.result.user.GetUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhaijun
 * @version :1.0 UserInfoManager.java, v 0.1 2023-04-10 9:55 zhaijun Exp $
 */
@Component
public class UserInfoManager {

    @Autowired
    UserInfoService userInfoService;

    /**
     * 获取用户信息
     *
     * @param request 请求
     * @return {@link GetUserInfoResult}
     */
    public GetUserInfoResult getUserInfo(GetUserInfoRequest request) {
        //结果
        GetUserInfoResult result = new GetUserInfoResult();
        Long id = request.getId();
        String name = request.getName();
        Integer maxAge = request.getMaxAge();
        String deptName = request.getDeptName();
        //用户信息列表
        List<UserInfoDO> userInfoList;
        //获取用户信息列表 方法1  mybatisPlus 用法
 /*       userInfoList = userInfoService.lambdaQuery()
                .eq(ObjectUtil.isNotNull(id), UserInfoDO::getId, id)
                .eq(StrUtil.isNotBlank(name), UserInfoDO::getName, name)
                //年龄小于等于
                .le(ObjectUtil.isNotNull(maxAge), UserInfoDO::getAge, maxAge)
                //部门名称模糊查询
                .like(StrUtil.isNotBlank(deptName), UserInfoDO::getDeptName, name)
                .list();*/

        //获取用户信息列表 方法2  常规mybatis用法
        userInfoList = userInfoService.getUserInfo(id, name, maxAge, deptName);
        //结果模型
        List<GetUserInfoVO> voList = CollUtil.newArrayList();
/*        //结果模型转换  方法1 stream流式转换
        voList = userInfoList.stream()
                .map(u -> {
                    GetUserInfoVO vo = new GetUserInfoVO();
                    vo.setName(u.getName());
                    vo.setPhone(u.getPhone());
                    return vo;
                }).collect(Collectors.toList());*/

        //结果模型转换  方法2 常规for循环转换
        for (UserInfoDO userInfoDO : userInfoList) {
            GetUserInfoVO vo = new GetUserInfoVO();
            vo.setName(userInfoDO.getName());
            vo.setPhone(userInfoDO.getPhone());
            voList.add(vo);
        }
        result.setDataList(voList);
        return result;
    }
}